package gw.com.android.app.dispatch;

import android.app.Activity;
import android.text.TextUtils;

import java.util.Map;

import gw.com.android.app.AppContances;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.model.DataManager;
import gw.com.android.terminal.GTSDataListener;
import gw.com.android.ui.MainActivity;
import gw.com.android.ui.dialog.LoginDialog;
import gw.com.android.ui.dialog.PopupConfirmDialog;
import gw.com.android.ui.quote2.QuoteChartConfig;
import gw.com.android.ui.trade.OrderOpenDialog;
import www.com.library.app.AppActivities;
import www.com.library.app.ObjectSessionStore;
import www.com.library.model.DataItemDetail;

/**
 * @author jett
 * @since 2018-05-16.
 */
public class TradeDispatch implements Dispatch {

    @Override
    public boolean handle(Activity activity, String path, Map<String, String> params) {
        if ("create_order".equals(path)) {
            createOrder(activity, params);
            return true;
        } else if ("tab".equals(path)) {
            toTradeTab();
            return true;
        }
        return false;
    }

    /**
     * hxgts2://trade/tab/
     */
    private void toTradeTab() {
        AppActivities.getSingleton().popAllActivityExceptOne(MainActivity.class.getSimpleName());
        MainActivity mainActivity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
        if (mainActivity != null) {
            mainActivity.setTabFragment(ConfigType.TAB_TRADE_TAG, "");
        }
    }

    /**
     * hxgts2://trade/create_order?code=123&trade_type=1&from=live
     * 下单。。
     */
    private void createOrder(Activity activity, Map<String, String> params) {
        if ((GTConfig.instance().getAccountType() != GTConfig.ACC_TYPE_GUEST)) {
            String code = params.get("code");
            String type = params.get("trade_type");
            String from = params.get("from");
            if (TextUtils.isEmpty(code) || TextUtils.isEmpty(from)) {
                return;
            }
            int uiCode;
            int tradeType;
            try {
                uiCode = Integer.parseInt(code);
                tradeType = TextUtils.isEmpty(type) ? AppContances.TRADE_TYPE_SALE : Integer.parseInt(type);
            } catch (NumberFormatException ignore) {
                return;
            }
            if (tradeType != AppContances.TRADE_TYPE_BUY && tradeType != AppContances.TRADE_TYPE_SALE) {
                return;
            }
            DataItemDetail model = DataManager.instance().getTickModel(uiCode);
            if (null != model) {
                if ("live".equals(from)) {
                    new OrderOpenDialog(activity, tradeType, uiCode, QuoteChartConfig.LONDON_GOLD, QuoteChartConfig.LONDON_SILVER).show();
                } else {
                    new OrderOpenDialog(activity, tradeType, uiCode).show();
                }
            } else {
                final PopupConfirmDialog dialog = PopupConfirmDialog.newInstance(activity, "", ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1021));
                dialog.show();
            }
        } else {
            // 游客登录时弹出提示
            LoginDialog.showLoginDialog(activity, ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1013));
        }
    }

}
